package com.financing.wap.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;



public class SMSSendUtil
{

	//	http://api.cnsms.cn/?ac=send&uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
	private String urlStr = "http://api.cnsms.cn";
	private String uid = PropertiesUtils.getValue("otp_uid"); //用户账号
	private String pwd = PropertiesUtils.getValue("otp_pwd");//MD5位32密码
	private String ac = "send";
	private String encode = "utf8";


	public String sendSMS(List<String> mobileList, String content)throws Exception
	{
		StringBuffer mobiles = new StringBuffer();
		for (String mobile : mobileList) {
			mobiles.append(mobile);
			mobiles.append(",");
		}
		String mobilesStr = mobiles.toString();
		mobilesStr = mobilesStr.substring(0, mobilesStr.length() - 1);
		//封装参数
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
	    nvps.add(new BasicNameValuePair("uid", this.uid));
	    nvps.add(new BasicNameValuePair("pwd", this.pwd));
	    nvps.add(new BasicNameValuePair("ac", this.ac));
	    nvps.add(new BasicNameValuePair("encode", this.encode));
	    nvps.add(new BasicNameValuePair("mobile", mobilesStr));
	    nvps.add(new BasicNameValuePair("content", content));
		//获取client对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(urlStr);
	    httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
		CloseableHttpResponse httpResponse  = httpclient.execute(httpPost);
		
//		InputStream is = httpResponse.getEntity().getContent();
//		BufferedReader br = new BufferedReader(new InputStreamReader(is));
//		StringBuffer resultSringBuffer = new StringBuffer();
//		String result = null;
//		while ((result = br.readLine()) != null) {
//			resultSringBuffer.append(result);
//		}
		
		try {
			System.out.println(httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			
			System.out.println("======================");
			InputStream is = httpResponse.getEntity().getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			StringBuffer resultSringBuffer = new StringBuffer();
			String result = null;
			while ((result = br.readLine()) != null) {
				resultSringBuffer.append(result);
			}
			System.out.println("======================"+result);
			
			// do something useful with the response body
			// and ensure it is fully consumed
			//消耗掉response
			EntityUtils.consume(entity);
		} finally {
			httpResponse.close();
			
		}
		return "";
	}



	/**
	 *
	 * @param plainText
	 *            明文
	 * @return 32位密文
	 */
	public String encryption(String plainText) {
		String re_md5 = new String();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			re_md5 = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return re_md5;
	}

	public static void main(String[] args)
			throws Exception
	{
		SMSSendUtil u = new SMSSendUtil();
		String pwd = u.encryption("123456");
		System.out.println(pwd);
		List l = new ArrayList();
		l.add("18321248185");
		
		u.sendSMS(l,"我要用很长很长的一段文字进行测试，这段管一点不都不长啊，看看测试的情况如何，【我是大括号】，{我是花括号}，(我是小括号)");
	}
}